﻿/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using PsTFS.Objects;
using PsTFS.Objects.Methodes;

namespace PsTFS.Commandes.Get
{
    /// <summary>
    /// Récupération d'un WorkItem
    /// </summary>
    [Cmdlet(VerbsCommon.Get, "WI")]
    public class Get_Wi: PsCmdlet
    {
        /// <summary>
        /// Constructeur et initialisaiton des données
        /// </summary>
        public Get_Wi()
        {
            this.ID = 0;
            this.Revision = 0;
            this.Date = DateTime.Now;
        }
        /// <summary>
        /// Lancement du processus
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
                WorkItemStore witstore = Methode_Service.WIS();

                if (ID != 0 && Revision == 0 && Date == DateTime.Now)
                {
                    //Retourne l'assembly compilée
                    WriteObject(witstore.GetWorkItem(ID));
                }
                if (ID != 0 && Revision != 0)
                {
                    //Retourne l'assembly compilée
                    WriteObject(witstore.GetWorkItem(ID, Revision));
                }

                if (ID != 0 && Date != DateTime.Now)
                {
                    //Retourne l'assembly compilée
                    WriteObject(witstore.GetWorkItem(ID, Date));
                }
                if (URI != null && Revision == 0 && Date == DateTime.Now)
                {
                    //Retourne l'assembly compilée
                    WriteObject(witstore.GetWorkItem(URI));
                }
                if (URI != null && Revision != 0)
                {
                    //Retourne l'assembly compilée
                    WriteObject(witstore.GetWorkItem(URI, Revision));
                }

                if (URI != null && Date != DateTime.Now)
                {
                    //Retourne l'assembly compilée
                    WriteObject(witstore.GetWorkItem(URI, Date));
                }
            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex,Error.GetInstance().GetError(this), ErrorCategory.WriteError, null);
                WriteError(err);
                Log.AddLog(ex.StackTrace);
            }
            finally
            {
                base.ProcessRecord();
            }

        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            Log.AddLog(null, "Get-WI is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server
        /// </summary>
        [Parameter(Position = 0)]
        public TeamFoundationServer TFS
        {
            set { Context.GetContext().Tfs = value; }
        }

        /// <summary>
        /// ID du workitem
        /// </summary>
        [Parameter(Position = 1)]
        [ValidateNotNullOrEmpty]
        public int ID { get; set; }

        /// <summary>
        /// Numero de révision
        /// </summary>
        [Parameter(Position = 2)]
        [ValidateNotNullOrEmpty]
        public int Revision { get; set; }

        /// <summary>
        /// Date de la version
        /// </summary>
        [Parameter(Position = 3)]
        [ValidateNotNullOrEmpty]
        public DateTime Date { get; set; }

        /// <summary>
        /// Uri du workitem
        /// </summary>
        [Parameter(Position = 3)]
        [ValidateNotNullOrEmpty]
        public Uri URI { get; set; }
    }
}